/// <reference path="../../../../../typings/index.d.ts" />
'use strict';
define([
    'angular',
], function(angular) {
    angular
        .module('myApp.services.getApiServices', [])
        .service('ApiIterface', [
            '$http',
            '$q',
            function($http, $q) {
                const getTaskList = () => {
                    let def = $q.defer();
                    $http.get('/task')
                        .success(data => def.resolve({ err: null, data: data }))
                        .error(err => def.resolve({ err: err, data: null }));
                    return def.promise;
                };
                const getListByParams = (querystr) => {
                    let def = $q.defer();
                    $http.post('/search', querystr)
                        .success(data => def.resolve({ err: null, data: data }))
                        .error(err => def.resolve({ err: err, data: null }));
                    return def.promise;
                };
                const AddTask = (Text) => {
                    let def = $q.defer();
                    $http
                        .post('/task', { task: Text })
                        .success(data => {
                            def.resolve({ err: null, data: data });
                        })
                        .error(err => def.resolve({ err: err, data: null }));
                    return def.promise;
                };
                const DelTask = (task) => {
                    let def = $q.defer();
                    $http
                        .delete(`/task/${task._id}`)
                        .success(data => {
                            def.resolve({ err: null, data: data })
                        })
                        .catch(err => def.resolve({ err: err, data: null }))
                    return def.promise;
                };
                const UpdateTask = (task) => {
                    let def = $q.defer();
                    $http
                        .post('/update', task)
                        .success(data => def.resolve({ err: null, data: data }))
                        .error(err => def.resolve({ err: err, data: null }))
                    return def.promise;
                };
                const register = (userinfo) => {
                    let def = $q.defer();
                    $http
                        .post('/register', userinfo)
                        .success(data => def.resolve({ err: null, data: data }))
                        .error(err => def.resolve({ err: err, data: null }))
                    return def.promise;
                };
                const login = (userinfo) => {
                    let def = $q.defer();
                    $http
                        .post('/login', userinfo)
                        .success(data => def.resolve({ err: null, data: data }))
                        .error(err => def.resolve({ err: err, data: null }))
                    return def.promise;
                };
                const logout = () => {
                    let def = $q.defer();
                    $http
                        .get('/logout')
                        .success(
                            data => def.resolve({ err: null, data: data })
                        )
                        .error(err => def.resolve({ err: err, data: null }));
                    return def.promise;
                }
                return {
                    getTaskList: getTaskList,
                    AddTask: AddTask,
                    DelTask: DelTask,
                    UpdateTask: UpdateTask,
                    getListByParams: getListByParams,
                    register: register,
                    login: login,
                    logout: logout,
                }

            }
        ])

});